package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.DataMap;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbGreenPlotApplyMapper;
import com.zfei.gfb.dao.GfbGreenPlotMapper;
import com.zfei.gfb.dao.GfbHouseApplyMapper;
import com.zfei.gfb.model.GfbGreenPlot;
import com.zfei.gfb.model.GfbGreenPlotApply;
import com.zfei.gfb.model.GfbHouseApply;
import com.zfei.gfb.query.GfbGreenPlotApplyQuery;
import com.zfei.gfb.query.GfbHouseApplyQuery;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "菜地认领列表接口")
@RestController
@RequestMapping("/web/plot/apply")
public class GfbGreenPlotApplyController  {

	@Resource
	private GfbGreenPlotApplyMapper gfbGreenPlotApplyMapper;

	@Resource
	private GfbGreenPlotMapper gfbGreenPlotMapper;

	@ApiOperation(value = "预约列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbGreenPlotApplyListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody GfbGreenPlotApplyQuery query) {
		query.build(Now::millis);
		//
		GfbGreenPlotApplyListVo result = new GfbGreenPlotApplyListVo();

		int total = gfbGreenPlotApplyMapper.total(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbGreenPlotApply> records = gfbGreenPlotApplyMapper.list(query);
		List<GfbGreenPlotApplyVo> vos  = Beans.copys(records,GfbGreenPlotApplyVo.class);
		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = GfbGreenPlotApplyVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbGreenPlotApply record = gfbGreenPlotApplyMapper.selectByPrimaryKey(id);
		if(record == null){
			throw new StatusException(Status.NOT_EXIST);
		}
		GfbGreenPlotApplyVo vo = Beans.copy(record, GfbGreenPlotApplyVo.class);


		GfbGreenPlot greenPlot = gfbGreenPlotMapper.selectByPrimaryKey(record.getPlotId());
		GfbGreenPlotVo greenPlotVo = Beans.copy(greenPlot, GfbGreenPlotVo.class);
		List<String> images = Gsons.fromJson(greenPlot.getImages(), Types.StringListType);
		greenPlotVo.setImages(images);

		vo.setGreenPlotVo(greenPlotVo);
		return vo;
	}

}
